package com.project.web.system.service.impl.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.common.core.utils.id.IDUtil;
import com.project.web.system.mapper.app.AdminRoleMapper;
import com.project.web.system.mapper.app.AdminMapper;
import com.project.web.system.model.app.AdminRole;
import com.project.web.system.model.app.Admin;
import com.project.web.system.service.app.AdminService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: wyy
 * @Date: 2020-04-14 00:36
 */
@Service("sysUserServiceImpl")
public class AdminServiceImpl implements AdminService {

    @Autowired
    protected AdminMapper sysUserMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * @param userId 主键
     * @return
     */
    @Override
    public int delete(String userId) {
        if (StringUtils.isBlank(userId)) {
            return 0;
        }
        return sysUserMapper.delete(userId);
    }

    /**
     * @param sysUser 对象
     * @return
     */
    @Override
    public Admin save(Admin sysUser) {
        Long maxUserId = sysUserMapper.getMaxUserId();
        Long id = maxUserId + 1;
        sysUser.setUserId(id);
        sysUser.setGmtCreate(new Date());
        String defautlPwd = DigestUtils.md5Hex(sysUser.getLoginPwd());
        sysUser.setLoginPwd(defautlPwd);
        sysUserMapper.save(sysUser);
        List<Long> roleIds = sysUser.getRoleIds();
        List<AdminRole> adminRoles = new ArrayList<>();
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setId(IDUtil.getId());
            adminRole.setRoleId(roleId);
            adminRole.setUserId(id);
            adminRole.setCreateDate(new Date());
            adminRoles.add(adminRole);
        }
        if (CollectionUtils.isNotEmpty(adminRoles)) {
            adminRoleMapper.batchSave(adminRoles);
        }
        return sysUser;
    }

    /**
     * @param userId 主键
     * @return
     */
    @Override
    public Admin getById(Long userId) {

        return sysUserMapper.getById(userId);
    }

    /**
     * @param sysUser
     * @return
     */
    @Override
    public Admin update(Admin sysUser) {
        sysUser.setGmtModified(new Date());
        sysUserMapper.update(sysUser);


        List<Long> roleIds = sysUser.getRoleIds();
        if (roleIds != null) {
            adminRoleMapper.deteleByAdminId(sysUser.getUserId());
            List<AdminRole> adminRoles = new ArrayList<>();
            for (Long roleId : roleIds) {
                AdminRole adminRole = new AdminRole();
                adminRole.setId(IDUtil.getId());
                adminRole.setRoleId(roleId);
                adminRole.setUserId(sysUser.getUserId());
                adminRole.setCreateDate(new Date());
                adminRoles.add(adminRole);
            }
            if (CollectionUtils.isNotEmpty(adminRoles)) {
                adminRoleMapper.batchSave(adminRoles);
            }

        }


        return sysUser;
    }

    /**
     * @param sysUser  对象
     * @param pageInfo 分页对象
     * @return
     */
    @Override
    public PageInfo<Admin> selectList(Admin sysUser, PageInfo<Admin> pageInfo) {
        if (pageInfo != null) {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        }
        return new PageInfo(sysUserMapper.selectList(sysUser));
    }

    /**
     * @param sysUser 对象
     * @return
     */
    @Override
    public List<Admin> selectList(Admin sysUser) {
        return sysUserMapper.selectList(sysUser);
    }

    /**
     * 根据用户登录名称查询用户
     *
     * @param userName 用户登录名
     * @return
     */
    @Override
    public Admin getByUserName(String userName) {
        return sysUserMapper.getByUserName(userName);
    }

    @Override
    public List<Admin> getTeachers() {
        return sysUserMapper.getTeachers();
    }
}
